package com.yzl.meeting.business.service.Impl;

import com.yzl.meeting.business.dao.MangeUserDao;
import com.yzl.meeting.business.dao.impl.MangeUserDaoImpl;
import com.yzl.meeting.business.dto.ShowUser;
import com.yzl.meeting.business.entity.Delegation;
import com.yzl.meeting.business.entity.PageBean;
import com.yzl.meeting.business.entity.Staff;
import com.yzl.meeting.business.service.MangeUserService;
import com.yzl.meeting.business.utils.MyDbUtils;
import com.yzl.meeting.business.vo.NewUserVo;

import java.sql.SQLException;
import java.util.List;

/**
 * @Title: MangeUserServiceImpl
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/10/14 11:01
 * @Version: 1.0
 */
public class MangeUserServiceImpl implements MangeUserService {
    MangeUserDao mangeUserDao=new MangeUserDaoImpl();
    @Override
    public List<Delegation> listDelegation() throws Exception{
        return mangeUserDao.listDelegation();
    }

    @Override
    public List<Staff> listStaff(Integer delegationId) throws Exception {
        return mangeUserDao.listStaff(delegationId);
    }

    @Override
    public void addUser(NewUserVo newUserVo) throws SQLException {
        try {
            MyDbUtils.beginTransaction();
            Integer uId=mangeUserDao.addUser(newUserVo);
            mangeUserDao.addUserStaff(uId,newUserVo.getStaffId());
            MyDbUtils.commitTransaction();
        }catch (Exception e){
            MyDbUtils.rollbackTransaction();
            e.printStackTrace();
        }
    }

    @Override
    public PageBean findAllUser(PageBean pageBean) throws SQLException {
        return mangeUserDao.findAllUser(pageBean);
    }

    @Override
    public void deleteUser(Integer uId) throws Exception{
        try {
            MyDbUtils.beginTransaction();
            mangeUserDao.deleteUserRole(uId);
            mangeUserDao.deleteUser(uId);
            MyDbUtils.commitTransaction();
        }catch (Exception e){
            MyDbUtils.rollbackTransaction();
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    @Override
    public NewUserVo findUserById(Integer uId) throws SQLException {
        return mangeUserDao.findUserById(uId);
    }

    @Override
    public void updateUser( NewUserVo newUserVo) throws Exception {
        try {
            MyDbUtils.beginTransaction();
            mangeUserDao.updateUser(newUserVo);
            mangeUserDao.updateUserStaff(newUserVo.getUid(),newUserVo.getStaffId());
            MyDbUtils.commitTransaction();
        }catch (Exception e){
            MyDbUtils.rollbackTransaction();
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    @Override
    public List<Staff> listAllStaff() throws SQLException {
        return mangeUserDao.listAllStaff();
    }
}
